/********** Summary Stats of High and Low Dependency Sections *******/

clear all
set more off
set scheme plotplainblind


/* import data */
import delimited "$Data/Original/Time Use Data.csv", case(upper) 

/* Clean Data */
 foreach v of varlist _all {
      capture rename `v' `= proper("`v'")'
   }

foreach var of varlist(Lines-Sectionjob){
replace `var' = proper(`var')
}

/* Assign proper section by exact job (the level at which ratings were given) */
replace Section = "Forming" if inlist(Lines, "Ll1", "Ll2") & Sectionjob == "Batching & Cream Mixing"
replace Section = "Cooling" if inlist(Lines, "Celebration") & Sectionjob ==  "Tray Cleaning And Putting Back(Tray)"
replace Section = "Maida Room" if Lines == "Maida Room"
replace Section = "Syrup Room" if Lines == "Syrup Room"
replace Section = "Box Machine" if inlist(Lines, "Ll1", "Ll2") & Sectionjob == "Box Making (Packaging)"

/* Remove sections if workers not surveyed */
drop if inlist(Section,  "Syrup Room")


/* Converting Interaction Strings into Numeric */
strip Coordination, of(".") generate(w_interact)




gen w_intmins = substr(w_interact, 1, 2) 
gen w_intsec = substr(w_interact, 3, 2)


strip Nonworkinteraction, of(".") generate(nw_interact)

gen nw_intmins = substr(nw_interact, 1, 2) 
gen nw_intsec = substr(nw_interact, 3, 2)

foreach var of varlist(w_intmins-nw_intsec){
destring `var', gen(`var'_num)
}


drop w_interact nw_interact

gen w_interact_secs = 60*w_intmins_num + w_intsec_num
gen nw_interact_secs = 60*nw_intmins_num + nw_intsec_num


/*** Final values ***/
gen w_int_mins = w_interact_secs/60
gen nw_int_mins = nw_interact_sec/60


* Collapse at the section level
* rename RoomtemperatureÂC Roomtemperaturec
collapse (mean) w_int_mins nw_int_mins Avgnoisedb Roomtemperaturec Directdependency, by(Lines Section)

** Table B2 **
dmout w_int_mins nw_int_mins Avgnoisedb  Roomtemperaturec using "$Output/Tables/dd_sum", by(Directdependency) tex list replace

** Figure 2 **
hist w_int_mins, bin(15) frequency ytitle("Number of Production Sections") xtitle("Direct Dependency: Minutes of continuous coordination (out of 10)") 
graph export "$Output/Figures/DD_Dist.pdf", as(pdf) replace


********************************
******** Save Dep Values *******
******** by Line-Section *******
********************************


rename Lines Line
rename Section section


* Renaming Lines and Sections (to merge with line-section-level data) *
replace Line = "Layer Line" if inlist(Line, "Ll1") /* Values same for Ll2 and Ll2 */
replace Line = "Old Layer" if inlist(Line, "Swiss Roll")


replace section = "Tray" if inlist(section, "Tray Wash", "Tray/Cooling Net")
replace section = "Fline" if inlist(section, "1St Line")
replace section = "Sline" if inlist(section, "2Nd Line")
replace section = "Injector" if inlist(section, "Inject")

keep Line section w_int_mins
la var w_int_mins "Dependency (Mins)"

save "$Data/Original/line_section_dependency_mins.dta", replace

** Values for Table 2 (Changing section names to have the same name if they have the same function) **
replace section = "Deposit" if inlist(section, "Fline", "Sline")
replace section = "Injector" if inlist(section, "Cream")
replace section = "Cfc" if inlist(section, "Box Filling", "Box Machine")

keep if inlist(section, "Mixing", "Deposit", "Oven", "Tray", "Injector", "Depanning", "Packing", "Cfc") /* Core sections */


collapse (mean) w_int_mins (p25) p25_w_int_mins=w_int_mins (p75) p75_w_int_mins=w_int_mins, by(section) /* Averaged across all lines (For Table 1) */ 
br 
